package com.jiuzhe.commercial.ui.my;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.adapter.AccountListAdapter;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.AccountListResult;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient2;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huanghao on 2018/6/22.
 */

public class DetailListActivity extends BaseActivity{
    @BindView(R.id.listview)
    RecyclerView recyclerView;
    @BindView(R.id.iv_not)
    ImageView ivNot;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    private List<AccountListResult> list = new ArrayList<>();
    private AccountListAdapter mAccountAdapter;
    private int page = 0;
    private int size = 8;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_account);
        setTitle("提现明细");
        intData();
        initRefresh();
        loadData2(page,size);
    }



    private void initRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 0;
                        loadData2(page,size);
                        refreshLayout.finishRefresh();
                        refreshLayout.resetNoMoreData();
                    }
                },1500);
            }
        });

       /* refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
               if( mAccountAdapter.getItemCount() > 0){
                    page++;
                    loadData2(page,size);
                    refreshLayout.finishLoadmore();
                }else {
                   refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                   Toast.makeText(getApplication(), "我是有底线的", Toast.LENGTH_SHORT).show();
               }
            }
        });*/
    }

    private void intData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(DetailListActivity.this));
        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mAccountAdapter = new AccountListAdapter(list);
        recyclerView.setAdapter(mAccountAdapter);
    }

    private void loadData2(int page, int size) {
        list.clear();
        HashMap<String, String> map = new HashMap<>();
        String id = SPUtil.getString(this, Constants.ID);
        String mr_id = SPUtil.getString(this, Constants.MERCHANTID);
        String token = SPUtil.getString(this, Constants.TOKEN);
        String autoSignStr = Constants.GETMWITHDRAW.substring(Constants.GETMWITHDRAW.lastIndexOf("0/")+1)  + mr_id + "/" + page +  "/" + size + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);

        String url = Constants.GETMWITHDRAW  + mr_id  + "/" + page +  "/" + size + "?token=" + id + "&&sign=" + signStr;

        HttpClient2.get(this, url , map, new CallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> result){
                if(result.get(0).equals("31")){//查询成功
                    List<AccountListResult> accountListResults = JSON.parseArray(result.get(2), AccountListResult.class);
                    list.addAll(accountListResults);
                    if (list != null && list.size() > 0){
                        mAccountAdapter.notifyDataSetChanged();
                    }else {
                        ivNot.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    protected void loadData() {

    }
}
